/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.controller;

import com.aptech.controller.util.PaginationHelper;
import com.aptech.entity.RealEstate;
import com.aptech.session.RealEstateFacadeLocal;
import com.aptech.session.RealEstateTypeFacadeLocal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author Tuan Nguyen
 */
@ManagedBean
@SessionScoped
public class SearchController {
    @EJB
    private RealEstateTypeFacadeLocal realEstateTypeFacade;

    @EJB
    private RealEstateFacadeLocal realEstateFacade;
    private RealEstate current;
    private int selectedItemIndex;
    private DataModel itemSearch = null;
    private PaginationHelper paginationSearch;
    int pageSize = 5;
    int realEstateTypeID;
    private int cityID;
    private int houseTypeID;
    private String location;
    private double priceMin;

    public RealEstate getSelected() {
        if (current == null) {
            current = new RealEstate();
            selectedItemIndex = -1;
        }
        return current;
    }
    public int getBathrooms() {
        return bathrooms;
    }

    public void setBathrooms(int bathrooms) {
        this.bathrooms = bathrooms;
    }

    public int getBedrooms() {
        return bedrooms;
    }

    public void setBedrooms(int bedrooms) {
        this.bedrooms = bedrooms;
    }

    public int getCityID() {
        return cityID;
    }

    public void setCityID(int cityID) {
        this.cityID = cityID;
    }

    public double getDeposit() {
        return deposit;
    }

    public void setDeposit(double deposit) {
        this.deposit = deposit;
    }

    public int getHouseTypeID() {
        return houseTypeID;
    }

    public void setHouseTypeID(int houseTypeID) {
        this.houseTypeID = houseTypeID;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public double getPriceMax() {
        return priceMax;
    }

    public void setPriceMax(double priceMax) {
        this.priceMax = priceMax;
    }

    public double getPriceMin() {
        return priceMin;
    }

    public void setPriceMin(double priceMin) {
        this.priceMin = priceMin;
    }

    public int getRealEstateTypeID() {
        return realEstateTypeID;
    }

    public void setRealEstateTypeID(int realEstateTypeID) {
        this.realEstateTypeID = realEstateTypeID;
    }
    private double priceMax;
    private double deposit;
    private int bedrooms;
    private int bathrooms;

    /** Creates a new instance of SearchController */
    public SearchController() {
    }

    public String search() {
        recreateModel();
        getItemSearch();
        return "ResultSearch";
    }

    public DataModel getItemSearch() {
        if (itemSearch == null) {
            itemSearch = getPaginationSearch().createPageDataModel();
        }
        return itemSearch;
    }

    public PaginationHelper getPaginationSearch() {
        if (paginationSearch == null) {
            paginationSearch = new PaginationHelper(pageSize) {

                @Override
                public int getItemsCount() {
                    return searchHouse().size();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(searchHouse(getPageFirstItem(), getPageFirstItem() + getPageSize()));
                }
            };
        }
        return paginationSearch;
    }

    public int totalPageSearch() {
        int size = searchHouse().size();
        return size % pageSize == 0 ? size / pageSize : size / pageSize + 1;
    }

    public String prepareView() {
        current = (RealEstate) getItemSearch().getRowData();
        selectedItemIndex = paginationSearch.getPageFirstItem() + getItemSearch().getRowIndex();
        return "DetailSearch";
    }

    public String viewPageSearch(int page) {
        getPaginationSearch().viewPage(page);
        recreateModel();
        return "ResultSearch";
    }
        public String next() {
        getPaginationSearch().nextPage();
        recreateModel();
        return "List";
    }

    public String previous() {
        getPaginationSearch().previousPage();
        recreateModel();
        return "List";
    }

    public List<RealEstate> searchHouse() {
        cityID = (current.getCityID() == null) ? 0 : current.getCityID().getCityID();
        houseTypeID = (current.getHouseTypeID() == null) ? 0 : current.getHouseTypeID().getHouseTypeID();
        realEstateTypeID = current.getRealEstateTypeID().getRealEstateTypeID();
        return realEstateFacade.findHouse(realEstateTypeID, cityID, location, priceMin, priceMax, deposit, bedrooms, bathrooms, houseTypeID);
    }

    public List<RealEstate> searchHouse(int from, int to) {
        List<RealEstate> list = searchHouse();
        List<RealEstate> listSub = new ArrayList<RealEstate>();
        for (int i = 0; i < list.size(); i++) {
            if (i >= from && i < to) {
                listSub.add(list.get(i));
            }

        }
        return listSub;
    }

    private void recreateModel() {
        itemSearch = null;
    }
}
